= new Scriptenginemanager ();//Only JavaScript actuators are called here, and JavaScript scripts can be executed,Other types of scripts can also be called scriptengine _enginer = _mgr.getenginebyname ("javascript"); try{String expression =Properties.getproperty ("Std_cache_time_out");Object result = _enginer.eval (expression);_max_cache_time_out = (long) double.parsedouble (result.tostring ());}}catch (Scriptexception e) {e.printstacktrace (); }See t
Example of using ScriptEngine to interact with javascript in java (New Features of JDK 6)
Code:
Package demo7;Import java. util. Arrays;Import java. util. List;Import javax. script. Invocable;Import javax. script. ScriptEngine;Import javax. script. ScriptEngineManager;Public class ScriptEngineTest {Public static void main (String [] args) throws Exception {ScriptEngineManager sem = new ScriptEngineManager (
The JDK1.6 version adds a new ScriptEngine class that allows the user to execute the JS code directly.
call JS code directly in Java
The JS function defined in the browser cannot be invoked, throwing an exception prompt referenceerror: "Alert" is not defined.
Package Com.sinaapp.manjushri;
Import Javax.script.ScriptEngine;
Import Javax.script.ScriptEngineManager;
Import javax.script.ScriptException; /** * Directly call JS code
/publ
Engine instance, you can easily execute the script language. By convention, we start with a simple hello World:
Public class runjavascript {Public static void main (string [] ARGs ){Scriptenginemanager factory = new scriptenginemanager ();Scriptengine engine = factory. getenginebyname ("JavaScript ");Engine. eval ("Print ('Hello World ')");}}
This Java code will execute JavaScript and print out Hello world. What if a syntax error occurs in JavaScript
engines, respectively, by name, file extension, and MIME type. Such as
Copy Code code as follows:
public void greet () throws Scriptexception {
Scriptenginemanager manager = new Scriptenginemanager ();
ScriptEngine engine = Manager.getenginebyname ("JavaScript");
if (engine = = null) {
throw new RuntimeException ("Cannot find JavaScript language execution engine");
}
Engine.eval ("println (' Hello ');");
}
It can a
easier to operate. In addition, the script engine includes Error events and Timeout events, which can be registered based on actual usage. II. The script engine demonstrates that I have created a form program to test whether to enable the AllowUI attribute, set the timeout time, and select a method for calling the script engine. The code of the test program is long. The following lists the main parts: using System; using System. drawing; using System. collections; using System. componentModel;
scriptenginemanager (); // obtain all the Script Engine Factory list factories = manager. getenginefactories (); // This Is The New for statement syntax for Java SE 5 and Java SE 6 (scriptenginefactory Factory: factories) {// print the script information out. printf ("Name: % S % N" + "Version: % S % N" + "language name: % S % N" + "language version: % S % N" + "extensions: % S % N "+" MIME types: % S % N "+" names: % S % N ", factory. getenginename (), factory. getengineversion (), factory. ge
implement javax.script.ScriptEngine (for simplicity, can inherit Javax.script.AbstractScriptEngine) and javax.script.ScriptEngineFactory two interfaces. Of course, before you implement your own scripting language engine, go to scripting.dev.java.net project to see if someone has helped you do your job, so you can use it directly.
Scripting APIThe Scripting API is an API for scripting language programs in Java, where the Scripting API is found in Javax.script, and we use this API to write JavaS
= factory.getEngineByExtension ("js");// create engine by nameScriptEngine engine = factory.getEngineByMimeType ("application/javascript");The following code will print out all the scripting engines supported by the current JDK:new ScriptEngineManager();for (ScriptEngineFactory available : factory.getEngineFactories()) { System.out.println(available.getEngineName()); // 打印脚本具体名称信息 System.out.println(available.getNames());}You can see [nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScr
Build your own C # script with RoslynIn the next generation of C #, an important feature is the "Compiler as a service", which simply means that the compiler is opened up to one that can be invoked in code. Recently used the Microsoft release of the Project Roslyn CTP version, feeling is very powerful.To execute a C # script in your own code, start with the following steps.
Download the Roslyn CTP version and install it on the Microsoft website
Add references to Roslyn.Compilers.dll
do not work, such as MsgBox statements in VBScript, alert in JavaScript, and if the script that executes exceeds the number of milliseconds set by timeout. Also does not jump out of time reminders of the dialog box, vice versa; Reset the Language property clears the Addcode-loaded code; For the Timeout property, the ScriptControl examines the object's Allowui properties when a timeout occurs. Determines whether user interface elements are allowed to be displayed.
If the reader needs a more det
Description of the Script API running on JVM, jvmscriptapi descriptionBasic API description
Other languages can be executed on jvm. A script engine is required to execute other languages. JDK provides related APIs.
This API is suitable for any language that can run on the JVM.
· ScriptEngine is a script execution engine used to execute specified scripts. Based on several eval rewriting forms, you can know that it can be used to execute a script in
http://blog.csdn.net/xzyxuanyuan/article/details/8062887The JDK1.6 version adds a new ScriptEngine class that allows the user to execute the JS code directly.Invoking the JS code directly in JavaYou cannot invoke the JS function defined in the browser and throw an exception prompt referenceerror: "Alert" is not defined.
Package Com.sinaapp.manjushri;Import Javax.script.ScriptEngine;Import Javax.script.ScriptEngineManager;Import j
strName from the registry.
Syntax
WshShell. RegDelete strName
Parameters
StrName
If strName ends with a backslash (\), this method deletes the key instead of the value.
The strName parameter must start with the root key name of one of the following columns:
Short Root Key name long root key name
HKCU HKEY_CURRENT_USER
HKLM HKEY_LOCAL_MACHINE
HKCR HKEY_CLASSES_ROOT
HKEY_USERS
HKEY_CURRENT_CONFIG
Example
Set WshShell = Wscript. CreateObject ("Wscript. Shell ")
WshShell. RegDelete "HKCU \
JAR service discovery mechanism. It is possible-drop any JSR-223 compliant script engine in the CLASSPATH and access the same from your Java application S (much like JDBC drivers, JNDI implementations is accessed).So we can either use the JSR223 API to execute the script or, of course, execute it directly using the API provided by the scripting language. Let's look at the following separately.JSR-223by Scriptenginemanager#getenginebyname This API we can get a script engine that implements JSR-2
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.